package com.easy.dfcapi.service.impl;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.dfcapi.entity.ApiUser;
import com.easy.dfcapi.mapper.ApiSellerMapper;
import com.easy.dfcapi.mapper.ApiStoreMapper;
import com.easy.dfcapi.mapper.ApiUserMapper;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.res.SellerUserRes;
import com.easy.dfcapi.service.ApiSellerService;
import com.easy.dfcapi.utils.ApiSessionUser;
import com.easy.dfcapi.utils.ServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by VULCANTSeries on 2017/5/10.
 */
@Service
@Transactional(readOnly = true)
public class ApiSellerServiceImpl implements ApiSellerService {

    @Autowired
    private ApiSellerMapper sellerMapper;

    @Autowired
    private ApiUserMapper userMapper;

    @Autowired
    private ApiStoreMapper storeMapper;

    @Override
    public Result HomePage() {
        Integer askSize = 5; //风车话题前五
        Integer factorySize = 2;//好厂推荐 前4

        Result rt = new Result();
        try{
            Map<String,Object> map = new HashMap<String,Object>();

/*
        List<Map<String,Object>> bannerList = dfcAdvertisementMapper.getHomeBanner();////顶部轮播

        List<AskOther> askOtherList = askOtherMapper.getFactoryAskOther(askSize);//风车话题前五
        List<Map<String,Object>> banner1List = dfcAdvertisementMapper.getMoreBanner(80);////中部
        List<Map<String,Object>> niceFactoryList = sellerMapper.getNiceFactoryList(factorySize);//好厂推荐，订单最多的店铺

        if(bannerList == null)bannerList = new ArrayList();
        if(askOtherList == null)askOtherList = new ArrayList();
        if(banner1List == null)banner1List = new ArrayList();
        if(niceFactoryList == null)niceFactoryList = new ArrayList();

        map.put("banner",bannerList);
        map.put("banner1",banner1List);
        map.put("askOther",askOtherList);
        List list = new ArrayList();
        for(int i =0; i < niceFactoryList.size(); i++){
            Map<String,Object> factory = niceFactoryList.get(i);
            List<Map<String,Object>> productList = sellerMapper.getNiceProductByFactoryId((Integer)factory.get("factoryId"));
            factory.put("productList",productList);
            list.add(factory);
        }
        map.put("niceFactory",list);
*/

            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(map);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    public Result Cateories(Response<SellerUserRes> response) {
        Result rt = new Result();
        try{

            SellerUserRes data = response.getData(SellerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<Map<String,Object>> niceProductList = sellerMapper.getCateories(data.getTypeId());
            if(niceProductList == null)niceProductList = new ArrayList();
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(niceProductList);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    public Result Products(Response<SellerUserRes> response) {
        Result rt = new Result();
        try{
            SellerUserRes data = response.getData(SellerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            List<Map<String,Object>> niceProductList = sellerMapper.getProducts(data);
            if(niceProductList == null)niceProductList = new ArrayList();
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(niceProductList);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

}
